package com.anysoft.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anysoft.MentorConst;
import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.ListAnnouncemetsBean;
import com.anysoft.utils.PreferencesUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @ProjectName：ShareSdkTest
 * @ClassDescription：文字上下轮播
 * @CreateUser： yanbin
 * @CreateTime：2017-03-27 10:36
 * @ModifiedUser：
 * @ModifiedTime：
 * @ModifiedRemark：
 */

public class LooperTextView extends FrameLayout {

    //    private List<String> tipList;
    private int curTipIndex = -1;
    private long lastTimeMillis ;
    private static final int ANIM_DELAYED_MILLIONS = 5 * 1000;
    /**  动画持续时长  */
    private static final int ANIM_DURATION = 1* 1000;
    private static final String DEFAULT_TEXT_COLOR = "#bbbbbb";
    private static final int DEFAULT_TEXT_SIZE = 14;
    //    private TextView tv_tip_out,tv_tip_in;
    private LinearLayout ll_tip_out,ll_tip_in;
    private TextView tv_tip_out1,tv_tip_out2,tv_tip_in1,tv_tip_in2;
    private Animation anim_out, anim_in;

    Drawable drawable;
    public LooperTextView(Context context) {
        super(context);
        initTipFrame();
        initAnimation();
    }

    public LooperTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTipFrame();
        initAnimation();
    }

    public LooperTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTipFrame();
        initAnimation();
    }
    private void initTipFrame() {
        drawable= getResources().getDrawable(R.drawable.ic_announcemets_point);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        ll_tip_out = newLinearLayout();
        ll_tip_in = newLinearLayout();
        tv_tip_out1 = newTextView();
//        tv_tip_out2 = newTextView();
        tv_tip_in1 = newTextView();
//        tv_tip_in2 = newTextView();
        ll_tip_out.addView(tv_tip_out1);
//        ll_tip_out.addView(tv_tip_out2);
        ll_tip_in.addView(tv_tip_in1);
//        ll_tip_in.addView(tv_tip_in2);
//        tv_tip_out = newTextView();
//        tv_tip_in = newTextView();
//        addView(tv_tip_in);
//        addView(tv_tip_out);
        addView(ll_tip_in);
        addView(ll_tip_out);
    }
    private LinearLayout newLinearLayout(){
        LinearLayout linearLayout = new LinearLayout(getContext());
        LayoutParams lp = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(lp);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        return linearLayout;
    }
    private TextView newTextView(){
        TextView textView = new TextView(getContext());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, 0);
        lp.weight = 1;
//        Drawable drawable= getResources().getDrawable(R.drawable.ic_announcemets_point);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(drawable,null,null,null);
        textView.setLayoutParams(lp);
        textView.setCompoundDrawablePadding(10);
        textView.setGravity(Gravity.CENTER_VERTICAL);
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setTextColor(Color.parseColor(DEFAULT_TEXT_COLOR));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_TEXT_SIZE);
        textView.setVisibility(GONE);
        return textView;
    }
    private void initAnimation() {
        anim_out = newAnimation(0, -1);
        anim_in = newAnimation(1, 0);
        anim_in.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                updateTipAndPlayAnimationWithCheck();
//                Log.e("testest",System.currentTimeMillis()+"");
            }
        });
    }
    private Animation newAnimation(float fromYValue, float toYValue) {
        Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,fromYValue,Animation.RELATIVE_TO_SELF, toYValue);
        anim.setDuration(ANIM_DURATION);
        anim.setStartOffset(ANIM_DELAYED_MILLIONS);
        anim.setInterpolator(new DecelerateInterpolator());
        return anim;
    }
    private void updateTipAndPlayAnimationWithCheck() {
        if (System.currentTimeMillis() - lastTimeMillis < 1000 ) {
            return ;
        }
        lastTimeMillis = System.currentTimeMillis();
        updateTipAndPlayAnimation();
    }
    boolean isShowTipIn = true;
    private void updateTipAndPlayAnimation() {
        if(isShowTipIn){
//        if (curTipIndex % 2 == 0) {
            updateTip(ll_tip_out);
//            tv_tip_in.startAnimation(anim_out);
//            tv_tip_out.startAnimation(anim_in);
//            this.bringChildToFront(tv_tip_in);
            ll_tip_in.startAnimation(anim_out);
            ll_tip_out.startAnimation(anim_in);
            this.bringChildToFront(ll_tip_in);
        } else {
            updateTip(ll_tip_in);
            ll_tip_out.startAnimation(anim_out);
            ll_tip_in.startAnimation(anim_in);
            this.bringChildToFront(ll_tip_out);
        }
        isShowTipIn =!isShowTipIn;
        iCurrTipIndex++;
    }
    int iCurrTipIndex = 0;

    public int getCurTipIndex(){
        if(mapTip==null || mapTip.size()==0){
            return -1;
        }
        int index = ((iCurrTipIndex-1)% mapTip.size());
        return index;
    }
    private void updateTip(LinearLayout tipView) {
        List<ListAnnouncemetsBean.DataBean> tipList = getNextTip();

        if(tipList!=null) {
            int childCount = tipView.getChildCount();
            if(childCount>0 && tipList.size()>0 && (tipView.getChildAt(0) instanceof TextView)){
                ((TextView)tipView.getChildAt(0)).setText(tipList.get(0).getTitle());
                ((TextView)tipView.getChildAt(0)).setVisibility(VISIBLE);
                setTextViewDrawableLeft((TextView) tipView.getChildAt(0),tipList.get(0).getId());
//                if(isRead(tipList.get(0).getId())){
//                    ((TextView)tipView.getChildAt(0)).setPadding(drawable.getMinimumWidth()+10,0,0,0);
//                    ((TextView)tipView.getChildAt(0)).setCompoundDrawables(null,null,null,null);
//                }else {
//                    ((TextView)tipView.getChildAt(0)).setCompoundDrawables(drawable,null,null,null);
//                    ((TextView)tipView.getChildAt(0)).setCompoundDrawablePadding(10);
//                }
//                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//
//                ((TextView)tipView.getChildAt(0)).setPadding(drawable.getMinimumWidth()+10,0,0,0);
//                ((TextView)tipView.getChildAt(0)).setCompoundDrawablePadding(10);
//                ((TextView)tipView.getChildAt(0)).setCompoundDrawables(null,null,null,null);
            }
//            if(childCount>1  && (tipView.getChildAt(1) instanceof TextView)){
//                if(tipList.size()>1) {
//                    ((TextView) tipView.getChildAt(1)).setText(tipList.get(1).getTitle());
//                    ((TextView)tipView.getChildAt(1)).setVisibility(VISIBLE);
//                    setTextViewDrawableLeft((TextView) tipView.getChildAt(1),tipList.get(1).getId());
////                    if(isRead(tipList.get(1).getId())){
////                        ((TextView)tipView.getChildAt(1)).setPadding(drawable.getMinimumWidth()+10,0,0,0);
////                        ((TextView)tipView.getChildAt(1)).setCompoundDrawables(null,null,null,null);
////                    }else {
////                        ((TextView)tipView.getChildAt(1)).setCompoundDrawables(drawable,null,null,null);
////                        ((TextView)tipView.getChildAt(1)).setCompoundDrawablePadding(10);
////                    }
//                }else {
//                    ((TextView)tipView.getChildAt(1)).setVisibility(GONE);
//                }
//            }
//            tipView.setText(tip);
        }else{
            int childCount = tipView.getChildCount();
            if(childCount>0 && (tipView.getChildAt(0) instanceof TextView)){
                ((TextView)tipView.getChildAt(0)).setVisibility(GONE);
            }
//            if(childCount>1  && (tipView.getChildAt(1) instanceof TextView)){
//                ((TextView)tipView.getChildAt(1)).setVisibility(GONE);
//            }
        }
    }
    private void setTextViewDrawableLeft(TextView textView,String sId){
        if(isRead(sId)){
            textView.setPadding(drawable.getMinimumWidth()+10,0,0,0);
            textView.setCompoundDrawables(null,null,null,null);
        }else {
            textView.setPadding(0,0,0,0);
            textView.setCompoundDrawables(drawable,null,null,null);
            textView.setCompoundDrawablePadding(10);
        }

    }
//    String sNextTip="";
    /**
     *  获取下一条消息
     * @return
     */
    private List<ListAnnouncemetsBean.DataBean> getNextTip() {
        if (mapTip == null || mapTip.size()<=0) return null;
        if(curTipIndex>=mapTip.size()){
            curTipIndex=0;
        }
//        sNextTip = mapTip.get(curTipIndex++);
        return mapTip.get(curTipIndex++);
    }
    public static boolean isListEmpty(List list) {
        return list == null || list.isEmpty();
    }

    BaseActivity activity;
    String sFundCustomerNo;
    private boolean isRead(String announcemetId){
        String sAlreadyRead = PreferencesUtils.getString(getContext(), MentorConst.READ_ANNOUNCEMET_ID,"");
        if(sAlreadyRead.contains(String.format("$%s$",announcemetId))){
            return true;
        }else{
            return false;
        }
    }

    private LinkedHashMap<Integer, List<ListAnnouncemetsBean.DataBean>> mapTip;
    public void setTipList(BaseActivity activity,List<ListAnnouncemetsBean.DataBean> tipList) {
        this.activity = activity;
        this.sFundCustomerNo = sFundCustomerNo;
//        this.tipList = tipList;

        isShowTipIn = true;
        iCurrTipIndex = 0;
        curTipIndex = 0;
        this.bringChildToFront(ll_tip_in);

        if(tipList==null || tipList.size()==0){
            if(mapTip!=null){
                mapTip.clear();
                updateTip(ll_tip_in);
            }
            return;
        }
        if(activity==null || activity.mentorApplication ==null || activity.mentorApplication.userBean==null){
            sFundCustomerNo = "";
        }else{
            sFundCustomerNo =  activity.mentorApplication.userBean.getFundCustomerNo();
        }
        mapTip = new LinkedHashMap<Integer, List<ListAnnouncemetsBean.DataBean>>();
        int index=0;
        List<ListAnnouncemetsBean.DataBean> itemList=null;
        for(int i=1;i<tipList.size()+1;i++){
//            if(i%2!=0){
                itemList = new ArrayList<ListAnnouncemetsBean.DataBean>();
//            }
            itemList.add(tipList.get(i-1));

//            if(i%2==0 || i== tipList.size()){
                mapTip.put(index++,itemList);
//            }
        }
        updateTip(ll_tip_in);

        // TODO: 2017/9/4 原来是大于1
        if(mapTip.size()>0){
            updateTipAndPlayAnimation();
        }
    }

}
